/**
 * 媒介管理初始化
 */
var Medium = {
    id: "MediumTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Medium.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', align: 'center', valign: 'middle', width: '50px'},
        {title: '媒介名称', field: 'mediumname', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'num', align: 'center', valign: 'middle', sortable: true},
    ];
};

/**
 * 检查是否选中
 */
Medium.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Medium.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
Medium.openAddMedium = function () {
    var index = layer.open({
        type: 2,
        title: '添加媒介',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/medium/medium_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看详情
 */
Medium.openMediumDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '媒介详情',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/medium/medium_update/' + Medium.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
Medium.delete = function () {
    if (this.check()) {

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/medium/delete", function () {
                Feng.success("删除成功!");
                Medium.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("mediumId", Medium.seItem.id);
            ajax.start();
        };

        Feng.confirm("是否刪除该媒介?", operation);
    }
};

/**
 * 查询列表
 */
Medium.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Medium.table.refresh({query: queryData});
};

Medium.resetSearch = function () {
    $("#condition").val("");
    PayService.search();
};


$(function () {
    var defaultColunms = Medium.initColumn();
    var table = new BSTreeTable(Medium.id, "/medium/list", defaultColunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("pid");
    table.setExpandAll(true);
    table.init();
    Medium.table = table;
});
